package com.asset.dispoal.user.service.commom;

import com.asset.dispoal.commom.utils.AES;
import com.asset.dispoal.commom.utils.MD5;
import com.asset.dispoal.user.domain.db.entity.SysUserDo;
import com.asset.dispoal.user.domain.db.mapper.SysUserMapper;
import com.asset.dispoal.user.dto.UserResetPwdRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.sugar.Attempt;
import tech.mhuang.pacebox.springboot.protocol.Result;

@Component
public class ResetPwdUserCommand implements BaseCommand<UserResetPwdRequest, Result> {

    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public Result execute(UserResetPwdRequest request) {
        SysUserDo userDo = sysUserMapper.getById(request.getId());
        String salt = userDo.getAccount().substring(userDo.getAccount().length() - 2);
        String password = Attempt.supply(() -> MD5.getMd5Code(AES.encrypt(salt, request.getPassword()), 1)).get();
        sysUserMapper.updatePwd(request.getId(), password);
        return Result.ok();
    }
}
